// 店铺销量排行榜
<template>
    <div class="gradient-content store full-width full-height">
        <div :id="idName"></div>
    </div>
</template>

<script>
import { Chart } from '@antv/g2'
export default {
    props: {
        idName: {
            type: String,
            default: 'storeSalesRanking',
        },
        chartData: {
            type: Array,
            default: () => [],
        },
        height: {
            type: [String, Number],
            default: 300,
        },
    },
    watch: {
        chartData(val) {
            this.chart.changeData(val)
        },
    },
    data() {
        return {
            chart: null,
            // height: 0,
        }
    },
    mounted() {
        this.$nextTick(() => {
            // const boxContElement = document.querySelector(`.gradient-content.store`)
            // this.height = boxContElement.clientHeight
            this.render()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.destroy()
    },
    methods: {
        render() {
        console.log(window.innerWidth)
            // Step 1: 创建 Chart 对象
            this.chart = new Chart({
                container: this.idName,
                height: this.height,
                width: window.innerWidth<678 ? (window.innerWidth -40) :( this.chartData.length == 1 ? 500 : this.chartData.length > 3 ? 1000 : 750),
                // autoFit: true,
                padding: [30, 30, 45, 70],
            })
            // Step 2: 载入数据源
            this.chart.data(this.chartData)
            // this.chart.axis(true)

            // Step 3：创建图形语法，绘制条形图，由 total_price 和 shop_name 两个属性决定图形位置，shop_name 映射至 x 轴，total_price 映射至 y 轴

            this.chart.scale({
                total_price: {
                    alias: '销售额',
                    nice: true,
                },
            })
            this.chart.tooltip({
                showMarkers: false,
            })
            this.chart
                .interval()
                .position('shop_name*total_price')
                .size( window.innerWidth<678 ? 10 :50)
                // .label('total_price', {
                //     offset: 10,
                // })
                .color('#11a983')
            this.chart.interaction('element-active')

            // Step 4: 渲染图表
            this.chart.render()
        },
    },
}
</script>
